﻿var subs, zxList;

$(function () {
    if (!uid) {
        alert('非登录用户不能编辑自选行情，即将跳转到登录页面');
        setTimeout(function () {
            location.href = loginHmtl;
        }, 1500);
        return;
    }

    $('.main_r').on('click', 'span', function () {
        var _this = $(this), tmp = subs[_this.data('i')];
        var el = $('.main_l .cur span');

        if (_this.hasClass('on')) {
            //取消自选
            for (var i = 0; i < zxList.length; i++) {
                if (tmp.SYMBOL_CODE == zxList[i].SYMBOL_CODE) {
                    zxList.splice(i, 1);
                    _this.removeClass('on');

                    el.html(el.html() - 1);
                    if (el.html() == 0) {
                        el.removeClass('on');
                    }
                    break;
                }
            }
        } else {
            el.html(parseInt(el.html()) + 1).addClass('on');
            _this.addClass('on');
            zxList.push(tmp);
        }
        $('#zxCount').html(zxList.length);
    });

    $('.main_l .item').click(function () {
        var _this = $(this), count = 0, html = '', className
        $('.main_l .cur').removeClass('cur');
        _this.addClass('cur');

        subs = list[_this.data('name')];
        for (var i = 0; i < subs.length; i++) {
            className = '';
            for (var k = 0; k < zxList.length; k++) {
                if (subs[i].SYMBOL_CODE == zxList[k].SYMBOL_CODE) {
                    className = 'on';
                    count++;
                    break;
                }
            }
            html += '<div class="item">' + subs[i].SYMBOL_NAME + '<span data-i=' + i + ' class="' + className + '"></span></div>';
        }

        $('.main_r').html(html);

        if (count > 0) {
            _this.find('span').html(count).addClass('show');
        }

    });

    $('#btnSave').click(function () {
        ld();

        //jsonp不支持post提交，url长度受限，只能提取自选的SYMBOL_CODE保存
        var tmp = [];
        for (var i = 0; i < zxList.length; i++) {
            tmp.push(zxList[i].SYMBOL_CODE);
        }

        $.ajax({
            url: 'http://hej.shqlkj.com/api/zxsave',
            data: { uid: uid, data: JSON.stringify(tmp) },
            dataType: 'jsonp',
            jsonp: "callback",
            success: function (json) {
                lc();
                alert(json.data);
                if (json.data.indexOf('错误')==-1) {
                    setTimeout(function () {
                        location.href = $('#a_hq').attr('href');
                    }, 3000);
                }
            },
            error: function (json) {
                lc();
                alert('保存失败');
            }
        });
    });


    try {
        zxList = JSON.parse(sessionStorage.getItem('zxList'));
        if (!zxList) {
            zxList = []
        }
    } catch (e) {
        zxList = [];
    }

    //列出左侧数字
    for (var i = 0; i < zxList.length; i++) {
        var item = zxList[i], el;
        if (item.SYMBOL_CODE == 'CONC') {
            //原油特殊处理，纳入到全球现货
            el = $('#WGJS span');
        } else {
            el = $('#' + item.EXCH_CODE + ' span');
        }
        el.html(parseInt(el.html()) + 1).addClass('on');
    }
    $('#zxCount').html(zxList.length);

    $('.main_l .item').eq(0).click();
});